'use strict';




angular.module("easy_web/template/dialog.html", []).run(["$templateCache", function ($templateCache) {
    $templateCache.put("easy_web/template/dialog.html",
        "<div class='modal-header'>" +
        "<h3 class='modal-title'>{{title}}</h3>" +
        "</div>" +
        "<div class='modal-body'>" +
        "{{content}}" +
        "</div>" +
        "<div class=\"modal-footer\">" +
        "<button ng-repeat='button in buttons' class=\"btn btn-primary\" type=\"button\" ng-click=\"button.action()\">{{button.title}}</button>" +
        "</div>");
}]);



var easy_web_Services = angular.module('easy.web.services', ["ui.bootstrap", "easy_web/template/dialog.html"])
    .controller('AlertController', function ($scope, $uibModalInstance, config) {
        $scope.title = config.title;
        $scope.content = config.content;
        $scope.buttons = config.buttons($uibModalInstance);
    })
    .controller('ChangePasswordController', function ($scope, $uibModalInstance, $http, config) {
        $scope.url = RSUpdateUser;
        $scope.oldPassword = config.password;
        $scope.newPassword = "";
        $scope.confirmPassword = "";
        $scope.valueOk = false;
        $scope.username = config.username;

        $scope.ok = function () {

            var req = {
                method: 'POST',
                url: $scope.url,
                data: {
                    username: $scope.username,
                    oldPassword: $scope.oldPassword,
                    newPassword: $scope.newPassword
                }
            };
            $http(req).then(function (response) {
                $uibModalInstance.close('OK');
            }, function (response) {
                console.log(response);
                $uibModalInstance.dismiss(response.data.msg);
            });
        };
        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    }).run(function ($http) {
        $http.defaults.headers.common['X-CSRF-TOKEN'] = CSRF_Token;
    });

easy_web_Services.factory('easyService', function ($rootScope,$compile) {
    var easyService = {};

    easyService.addCss=function(base,file){
        var scope = $rootScope.$new();
        scope.cssUrl = easyService.staticResource(base,file);
        var html = '<link type="text/css" rel="stylesheet" ng-href="{{cssUrl}}" />';
        angular.element('head').append($compile(html)(scope));
    };

    easyService.staticResource = function(base,file){
        return ezStaticResource(base,file);
    };

    easyService.appROOT = function(){
        return APP_ROOT;
    };

    return easyService;
});

easy_web_Services.factory('dialogService', function ($rootScope, $uibModal, $http) {
    var dialogService = {};

    dialogService.open = function (size, data) {
        var modalInstance = $uibModal.open({
            //animation: $scope.animationsEnabled,
            templateUrl: "easy_web/template/dialog.html",
            controller: 'AlertController',
            size: size,
            resolve: {
                config: function () {
                    return data;
                }
            }
        });

        return modalInstance;
    };


    dialogService.alert = function (title, message, successCallback, cancelCallback) {
        var dialog = dialogService.open('sm', {
            title: title,
            content: message,
            buttons: function ($uibModalInstance) {
                return [
                    {
                        title: 'OK',
                        action: function () {
                            $uibModalInstance.close('OK');
                        }
                    },
                    {
                        title: 'Cancel',
                        action: function () {
                            $uibModalInstance.dismiss('cancel');
                        }
                    }
                ]
            }
        });

        dialog.result.then(function (selectedItem) {
            if (successCallback != undefined && successCallback != null)
                successCallback(selectedItem);
        }, function () {
            if (cancelCallback != undefined && cancelCallback != null)
                cancelCallback();
        });
    };

    dialogService.message = function (title, message, successCallback) {
        var dialog = dialogService.open('sm', {
            title: title,
            content: message,
            buttons: function ($uibModalInstance) {
                return [
                    {
                        title: 'OK',
                        action: function () {
                            $uibModalInstance.close('OK');
                        }
                    }
                ]
            }
        });

        dialog.result.then(function (selectedItem) {
            if (successCallback != undefined && successCallback != null)
                successCallback(selectedItem);
        }, function () {

        });
    };

    return dialogService;
});


easy_web_Services.factory('userService', function ($rootScope, $uibModal, $http, dialogService) {

    var userService = {};

    userService.current = function (sc, ec) {
        var userInfoURL = RSQueryCurrentUser;

        var req = {
            method: 'POST',
            url: userInfoURL,
            headers: {
                'X-CSRF-TOKEN': CSRF_Token
            }
        };

        $http(req).then(function (response) {
            if (sc != undefined || sc != null) {
                sc(response.data);
            }
        }, function (response) {
            if (ec != undefined || ec != null) {
                ec(response.data);
            }
        });
    };

    userService.logout = function () {

        dialogService.alert('Message', 'Do you want to logout?', function (selectedItem) {
            var req = {
                method: 'POST',
                url: APP_ROOT + "/logout",
            };
            $http(req).then(function () {
                window.location = APP_FULL;
            });
        });

    };

    userService.changePassword = function (successCallback, cancelCallback) {

        userService.current(function (response) {
            var username = response.username;
            var password = response.password;
            if (username == undefined || username == null) {
                dialogService.message("Message", "Get current user info Error!", function () {
                });
                return;
            }

            var modalInstance = $uibModal.open({
                templateUrl: APP_ROOT + "/static/default/template/changePassword.html",
                controller: 'ChangePasswordController',
                size: '',
                resolve: {
                    config: function () {
                        return {
                            username: username,
                            password: password
                        };
                    }
                }
            });

            modalInstance.result.then(function (selectedItem) {
                dialogService.message("Message", "Change password success!", function () {
                    if (successCallback != undefined && successCallback != null)
                        successCallback(selectedItem);
                });

            }, function (msg) {
                if (msg != 'cancel') {
                    dialogService.message("Change Password Error!", msg, function () {
                    });
                }
                if (cancelCallback != undefined && cancelCallback != null)
                    cancelCallback();
            });

        }, function (response) {
            dialogService.message("Message", "Get current user info Error!", function () {
            });
        });


    };

    return userService;

});

easy_web_Services.factory('systemService', function ($rootScope, $http) {
    var systemService = {};

    systemService.currentUserModules = function (sc, ec) {
        var url = RSQueryModules;

        var req = {
            method: 'POST',
            url: url,
            headers: {
                'X-CSRF-TOKEN': CSRF_Token
            }
        };

        $http(req).then(function (response) {
            if (sc != undefined || sc != null) {
                sc(response.data);
            }
        }, function (response) {
            if (ec != undefined || ec != null) {
                ec(response.data);
            }
        });
    };

    return systemService;
});


